package com.dy.security.config;

import com.dy.security.filter.VerificationCodeFilter;
import com.dy.security.service.impl.UserServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.annotation.Resource;


/**
 * @Author: ding-yu
 * @Date: 2022/5/19 17:24
 * @Desctiption: (描述)
 */
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Resource
    private UserServiceImpl userDetailsService;

    /**
     * 配置数据源
     * @param auth auth
     * @throws Exception Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService);
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                //放行注册接口
                .antMatchers("/kaptcha.jpg","/js/**","/images/**","/static/**","/user/register").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login.html")
                .loginProcessingUrl("/login").permitAll();
        //实现验证码过滤器添加
        http.addFilterBefore(new VerificationCodeFilter(), UsernamePasswordAuthenticationFilter.class);
        //解决login表单提交时，把POST提交转成GET,关闭跨域，不然POST请求过不来
        http.csrf().ignoringAntMatchers("/login").disable();
    }

    /**
     * 放行swagger和knife4j
     * @param web web
     * @throws Exception Exception
     */
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/swagger/**")
                .antMatchers("/swagger-ui.html")
                .antMatchers("/webjars/**")
                .antMatchers("/v2/**")
                .antMatchers("/v3/**")
                .antMatchers("/v2/api-docs-ext/**")
                .antMatchers("/swagger-resources/**")
                .antMatchers("/doc.html")
                //放行登录页静态资源
                .antMatchers("/js/**","/css/**","/images/**","/webjars/**","/static/**");
    }

    /**
     * 密码策略
     * @return PasswordEncoder
     */
    @Bean
    public PasswordEncoder passwordEncoder(){
        //1.不适使用密码加密
        //return NoOpPasswordEncoder.getInstance();


        //2.使用默认的BCryptPasswordEncoder加密方案
//        return new BCryptPasswordEncoder();


        //3.strength=10 秘钥的迭代次数为10（strength取值在4-31之间，默认为10）
        // return new BCryptPasswordEncoder(10);


        //4.利用工厂类PasswordEncoderFactories实现,工厂类内部采用的是委派密码编码方案.
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
    }

}
